package com.aierkeji.b2c.entity;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.aierkeji.b2c.utils.WebUtils;
import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 后台管理用户
 */

@SqlResultSetMapping(name = "AkjAuser", classes = { @ConstructorResult(targetClass = AkjAuser.class, columns = {
		@ColumnResult(name = "name", type = String.class), @ColumnResult(name = "id", type = String.class) }) })
@javax.persistence.NamedNativeQuery(name = "AkjAuser.findByLifeId", query = "select u.name,u.id from akj_auser_alipay_ref luref join "
		+ "akj_auser u on luref.au_id = u.id join " + "akj_auser_role_ref  arref on u .id = arref.au_id "
		+ "join akj_auser_role r on arref.r_id = r.id "
		+ "where r.code = :roles and luref.ali_id = :lifeId and (u.leader_id=:leaderId or 1= :isUser)", resultSetMapping = "AkjAuser")
@Entity
@Table(name = "akj_auser")
public class AkjAuser {

	@Id
	@GeneratedValue(generator = "uuid")
	@GenericGenerator(name = "uuid", strategy = "uuid")
	@Column(name = "id", length = 32)
	private String id;

	@Column(length = 27)
	private String name;

	@Column(length = 27)
	private String account;

	@Column(length = 128)
	private String pwd;

	@Column(columnDefinition = "datetime")
	private Timestamp createTime;

	@ManyToOne(targetEntity = AkjAuser.class, cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	@JoinColumn(name = "create_user_id", nullable = true)
	@NotFound(action = NotFoundAction.IGNORE)
	private AkjAuser createBy;

	@Column(length = 1)
	private Boolean isDelete;

	@Column(name = "leader_id", insertable = false, updatable = false)
	private String leaderId;

	@ManyToOne(targetEntity = AkjAuser.class, cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	@JoinColumn(name = "leader_id", nullable = true)
	@NotFound(action = NotFoundAction.IGNORE)
	private AkjAuser leader;

	@OneToMany(cascade = CascadeType.ALL, targetEntity = AkjAuserRole.class, fetch = FetchType.EAGER)
	@JoinTable(name = "akj_auser_role_ref", joinColumns = {
			@JoinColumn(name = "au_id", referencedColumnName = "id") }, inverseJoinColumns = {
					@JoinColumn(name = "r_id", referencedColumnName = "id") })
	private List<AkjAuserRole> roles;

	@Transient
	private Set<String> roleStringSet = new HashSet<>();

	@Transient
	private String token;

	public AkjAuser(String name, String id) {
		super();
		this.id = id;
		this.name = name;
	}

	public AkjAuser() {
		super();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	public Timestamp getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}

	public Boolean getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(Boolean isDelete) {
		this.isDelete = isDelete;
	}

	public String getLeaderId() {
		return leaderId;
	}

	public void setLeaderId(String leaderId) {
		this.leaderId = leaderId;
	}

	public AkjAuser getLeader() {
		return leader;
	}

	public void setLeader(AkjAuser leader) {
		this.leader = leader;
	}

	public List<AkjAuserRole> getRoles() {
		return roles;
	}

	public void setRoles(List<AkjAuserRole> roles) {
		this.roles = roles;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Set<String> getRoleStringSet() {
		return roleStringSet;
	}

	public void setRoleStringSet(Set<String> roleStringSet) {
		this.roleStringSet = roleStringSet;
	}

	public AkjAuser getCreateBy() {
		return createBy;
	}

	public void setCreateBy(AkjAuser createBy) {
		this.createBy = createBy;
	}

	public Boolean checkParamsNull() {
		return WebUtils.checkParamsIsNull(this.account, this.name, this.pwd);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		AkjAuser akjAuser = (AkjAuser) o;
		return Objects.equals(id, akjAuser.id) && Objects.equals(name, akjAuser.name)
				&& Objects.equals(account, akjAuser.account);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id, name, account);
	}
}
